package com.haian.slidealbum.model.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.haian.slidealbum.R;
import com.haian.slidealbum.common.UtilsCommon;
import com.haian.slidealbum.control.InstagramActivity;
import com.haian.slidealbum.model.object.ObjectInstagramImage;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class AdapterInstagram extends  ArrayAdapter<ObjectInstagramImage> {

	private Activity mActivity;
	private ArrayList<ObjectInstagramImage> listImage;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;

	@SuppressWarnings("deprecation")
	public AdapterInstagram(Activity _mActivity, ArrayList<ObjectInstagramImage> _listImage) {
		super(_mActivity, R.layout.item_gallery, _listImage);
		this.mActivity = _mActivity;
		this.listImage = _listImage;

		imageLoader = ImageLoader.getInstance();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				mActivity).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO).build();
		imageLoader.init(config);
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.img_default)
				.showImageForEmptyUri(R.drawable.img_default)
				.showImageOnFail(R.drawable.img_default).cacheInMemory()
				.cacheOnDisc().resetViewBeforeLoading(true) // default
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
				.bitmapConfig(Bitmap.Config.RGB_565) // default
				.displayer(new FadeInBitmapDisplayer(300)) // default
				.handler(new Handler()) // default
				.build();
	}

	@Override
	public int getCount() {
		return listImage.size();
	}

	@Override
	public ObjectInstagramImage getItem(int position) {
		return listImage.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		final Holder holder;
		if (view == null) {
			LayoutInflater inflater = mActivity.getLayoutInflater();
			view = inflater.inflate(R.layout.item_gallery, parent, false);

			holder = new Holder();
			holder.rlItem = (RelativeLayout)view.findViewById(R.id.rlItem);
			holder.itemCheckbox = (ImageView) view
					.findViewById(R.id.itemCheckbox);
			holder.itemCheckboxSelected = (ImageView) view
					.findViewById(R.id.itemCheckboxSelected);
			holder.itemImage = (ImageView) view.findViewById(R.id.itemImage);
			
			Point size = UtilsCommon.getSizeScreen(mActivity);
			holder.itemImage.getLayoutParams().height = size.x/3;
			
			view.setTag(holder);
		} else {
			holder = (Holder) view.getTag();
		}
		
		final ObjectInstagramImage objectImage = (ObjectInstagramImage) getItem(position);
		imageLoader.displayImage(objectImage.getImage_source(), holder.itemImage, options);
//		
//		File f = new File(objectImage.getUrlImage());
//		holder.itemImage.setImageBitmap(UtilsCommon.reSizeFile(f));
		
		if(objectImage.isSelected())
		{
			holder.itemCheckbox.setVisibility(View.INVISIBLE);
			holder.itemCheckboxSelected.setVisibility(View.VISIBLE);
		}else{
			holder.itemCheckbox.setVisibility(View.VISIBLE);
			holder.itemCheckboxSelected.setVisibility(View.INVISIBLE);
		}
		
		holder.rlItem.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(objectImage.isSelected())
				{
					holder.itemCheckbox.setVisibility(View.VISIBLE);
					holder.itemCheckboxSelected.setVisibility(View.INVISIBLE);
					objectImage.setSelected(false);
				}else{
					holder.itemCheckbox.setVisibility(View.INVISIBLE);
					holder.itemCheckboxSelected.setVisibility(View.VISIBLE);
					objectImage.setSelected(true);
				}
				((InstagramActivity)getContext()).getListImageSelected();
			}
		});
		return view;
	}

	class Holder {
		RelativeLayout rlItem;
		ImageView itemImage;
		ImageView itemCheckbox;
		ImageView itemCheckboxSelected;
	}

}
